package com.yl.chainStore.services.impl


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.baomidou.mybatisplus.core.toolkit.Wrappers
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.yl.chainStore.common.enums.TypeEnum
import com.yl.chainStore.entity.customer.CustomerInfo
import com.yl.chainStore.entity.pay.PayCompanyCollectionLog
import com.yl.chainStore.entity.system.CompanyDictionary
import com.yl.chainStore.mapper.customer.CustomerInfoMapper
import com.yl.chainStore.mapper.pay.PayCompanyCollectionLogMapper
import com.yl.chainStore.pojo.vo.customer.CustomerSelectVo
import com.yl.chainStore.pojo.vo.system.CompanyDictionaryVo
import com.yl.chainStore.services.ISysNumGeneratorService
import com.yl.chainStore.services.IWebCompanyService
import com.yl.chainStore.services.IWebSystemService
import com.yl.chainStore.util.setDayFirstTime
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import com.yl.users.entity.CompanyDept
import com.yl.users.mapper.CompanyDeptMapper
import com.yl.users.services.ISysCompanyService
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.*

/**
 * @description :公司实现类
 * @author :吴枫
 * @create :2021-07-06 16:07:00
 */
@Service
class WebCompanyServiceImpl(
	private val companyDeptMapper: CompanyDeptMapper,
	private val customerInfoMapper: CustomerInfoMapper,
	private val payCompanyCollectionLogMapper: PayCompanyCollectionLogMapper
) : IWebCompanyService {

	@Autowired
	lateinit var iSysCompanyService: ISysCompanyService

	@Autowired
	lateinit var iWebSystemService: IWebSystemService

	@Autowired
	lateinit var iSysNumGeneratorService:ISysNumGeneratorService



	companion object {
		private val log = LoggerFactory.getLogger(IWebCompanyService::class.java)
	}


	override fun getDictionaryList(
		page: Page<CompanyDictionary>,
		companyDictionary: CompanyDictionaryVo
	): Page<CompanyDictionary> {
		log.info("公司管理 ==> 数据字典列表： page:{},vo:{}",page,companyDictionary)
		return iWebSystemService.getCompanyDictionary(page, companyDictionary)
	}


	override fun getWarehouseList(companyId: Long): List<CompanyDept> {
		log.info("公司管理 ==> 仓库列表： companyId:{}",companyId)
		val mutableListOf = mutableListOf<CompanyDept>()
		val topDept = iSysCompanyService.getTopDept(companyId)
		mutableListOf.add(topDept)
        val selectList = companyDeptMapper.selectList(
            QueryWrapper<CompanyDept?>().eq("super_dept_id", topDept.id).eq("sub_company", 1)
        )
        mutableListOf.addAll(selectList)
        return mutableListOf

	}

	override fun getCustomerPage( customerInfo: CustomerSelectVo): Page<CustomerInfo> {
		log.info("公司管理 ==> 客户分页： customerInfo:{}",customerInfo)
		if(customerInfo.companyId == null){
			customerInfo.companyId = getContext().companyId
		}
		return customerInfoMapper.selectCompanyCustomerPage(Page<CustomerInfo>(customerInfo.current,customerInfo.size),customerInfo)
	}

	override fun getCustomerList(customerInfo: CustomerSelectVo): List<CustomerInfo> {
		log.info("公司管理 ==> 客户列表： customerInfo:{}",customerInfo)
		if(customerInfo.companyId == null){
			customerInfo.companyId = getContext().companyId
		}
		return customerInfoMapper.selectCustomerList(customerInfo)
	}

	override fun addCustomer(info: CustomerInfo): ResponseBody {
		log.info("公司管理 ==> 添加患者：info：{}；",info)
		if(info.birthDate != null){
			val toDay = Date()
			setDayFirstTime(toDay)
			if(toDay.before(info.birthDate)){
				return returnError(
					ErrorCode.DATA_SAVE_FAILED,
					"生日日期错误"
				)
			}
		}
		if(iSysNumGeneratorService.getSysNumIsUse(getContext().companyId,TypeEnum.SYS_NUM_GENERATOR_SICK,null,info.historySickNum)){
			return returnError(ErrorCode.DATA_SAVE_FAILED,"病历号码被使用")
		}
		// TODO: 2021/7/9 验证
		if(haveSameHistoryNum(info.historySickNum)){
			return returnError(ErrorCode.DATA_SAVE_FAILED,"存在相同病历号")
		}
		iSysNumGeneratorService.useSysNum(getContext().companyId,TypeEnum.SYS_NUM_GENERATOR_SICK,null,info.historySickNum)
		customerInfoMapper.insert(info)
		return returnSuccess()
	}

	override fun haveSameHistoryNum(historyNum: String): Boolean {

		return  customerInfoMapper.selectCount(Wrappers.query<CustomerInfo?>()
			.eq("history_sick_num",historyNum)
			.eq("company_id", getContext().companyId)) != 0
	}

	override fun collection(payCompanyCollectionLog: PayCompanyCollectionLog): ResponseBody {
		log.info("公司管理 ==> 收款：payCompanyCollectionLog：{}；",payCompanyCollectionLog)
		payCompanyCollectionLogMapper.insert(payCompanyCollectionLog)
		return returnSuccess()
	}




}
